function urlParse() {
    var search = location.search;
    var data = {};
    if (search) {
        var str = search.substr(1);
        var list = str.split("&");
        for (var i = 0; i < list.length; i++) {
            var key = list[i].split("=")[0];
            var val = list[i].split("=")[1];
        }
        data[key] = val;
    }
    return data;
}
var data = urlParse();
var id = data["goodsId"];
goodsDetail();
async function goodsDetail() {
    var result = await searchGoodsById({ id });
    var { status, msg, list } = result;
    if (status) {
        var { goodsId, goodsImg, goodsName, goodsPrice, store } = list;
        price = (goodsPrice * 1).toFixed(2);
        $(".storeName a").text(store);
        $(".goodsInfo").text(goodsName);
        $(".price").text(price);
        var html = `<li>
                        <img src="${goodsImg}" alt="" class="active">
                    </li>
                    `
        $(".showList").html(html);
        $(".specImg").prop("src", goodsImg);
        $(".bigImg").prop("src", goodsImg);
    }
}

var maxLeft = null;
var maxTop = null;
$(".showList").on("mouseenter", "li", function () {
    $(this).children("img").addClass("active").end().siblings().children("img").removeClass("active");
    $(".specImg").attr("src", $(this).children("img").attr("src"));
    $(".bigImg").attr("src", $(this).children("img").attr("src"));
})
$(".showGoods").mouseenter(function () {
    $(".shadow").css({ display: "block" });
    $(".scaleGoods").css({ display: "block" });
    maxLeft = $(".showGoods").prop("clientWidth") - $(".shadow").prop("clientWidth");
    maxTop = $(".showGoods").prop("clientHeight") - $(".shadow").prop("clientHeight");
})
$(".showGoods").mouseleave(function () {
    $(".shadow").css({ display: "none" });
    $(".scaleGoods").css({ display: "none" });
})
$(".showGoods").mousemove(function () {
    var e = e || window.event;
    var x = e.pageX - $(this).prop("offsetLeft") - $(".shadow").prop("clientWidth") / 2;
    var y = e.pageY - $(this).prop("offsetTop") - $(".shadow").prop("clientHeight") / 2;

    if (x < 0) x = 0;
    if (x > maxLeft) x = maxLeft;
    if (y < 0) y = 0;
    if (y > maxTop) y = maxTop;
    $(".shadow").css({ left: x });
    $(".shadow").css({ top: y });
    $(".bigImg").css({ left: -x * 2 });
    $(".bigImg").css({ top: -y * 2 });
})


$(".counts").val(1);
var num = $(".counts").val();
$(".add").click(function () {
    num++;
    $(".counts").val(num);
})
$(".reduce").click(function () {
    if ($(".counts").val() == 1) return false;
    num--;
    $(".counts").val(num);
})


$(".appendToCar").click(async function () {
    if (user) {
        var result = await appendShopCar({ user, id, num });
        var { status } = result;
        if (status) {
            $(".go").slideDown("normal");

            $(".confirm").click(function () {
                location.href = "../html/shoppingCar.html";
            })

            $(".abrogate").click(function () {
                $(".go").slideUp("normal");
                location.reload();
            })
        }
    } else {
        $(".loginPopUp").fadeIn("fast");

        $(".determine").click(function () {
            var lh = encodeURIComponent(location.href);
            location.href = "../html/login.html?returnUrl=" + lh;
        })
        $(".cancel").click(function () {
            $(".loginPopUp").fadeOut("fast");
        })
    }
})


var user = getCookie("lgc");
if (user) {
    $(".tips").html(`<a href="#">${user}</a> <span></span><div class="exit">退出</div>`).css({ fontFamily: "icomoon" });
    $(".tips").mouseenter(function () {
        $(".exit").css({ display: "block" });
    })
    $(".tips").mouseleave(function () {
        $(".exit").css({ display: "none" });
    })
}
$(".exit").click(function () {
    delCookie("lgc");
    location.reload();
})

$(".searchCon").focus(function () {
    $(this).addClass("inpl");
});
$(".searchCon").blur(function () {
    $(this).removeClass("inpl");
});

